const express = require('express');
const Router = express.Router();
var Logger = require('../server/logger');
var types = require('./dbTypes')
var upload = require('../server/multerUtil')

const logger = Logger.GetLogger('api/upload')

logger.info('api/upload 加载成功...')
let allFiles = []
let allPath = []
Router.post('/uploadImage',upload.array('uploadImage', 5), (req, res, next) => {
  // var uploadsUrl = path.join(__dirname, '../uploads/')
  allFiles.push(req.files)
  allFiles.forEach((file, index) => {
    allPath.push('/uploads/' + file[index].filename)
  })
  if(req.files.length > 0 && allFiles.length <= 5) {
    allFiles = []
    res.json({
      code: types.SUCCESS_CODE,
      url: allPath[allPath.length-1],
      message: types.SUCCESS_MESSAGE_UPLOAD
    })
  } else {
    allFiles = []
    res.json({
      code: types.ERROR_CODE,
      message: types.ERROR_MESSAGE_FAILE
    })
  }
})

module.exports = Router